import { request } from '@/utils/request/axios';
import { ErrorMessageMode, PageRequest, PageData, AjaxResult } from '#/request';

import { AppOutputDtoSimple } from '@/api/mobile/model/app';
import { UpgradeCheckDto, AppPackageResultDto } from '@/api/mobile/model/upgrade';

import { AdChooseList, AdChooseListDialog, useAdChooseList } from '@/components/AdChooseList';
import { AdColumn } from '@/components/AdVxeTable';
import { useI18n } from '@/hooks/web/useI18n';

const { t } = useI18n();

/** 移动端URL枚举 */
enum Api {
  /** 读取移动端应用 */
  ReadApp = '/api/mobile/readApp',
  /** 获取移动端应用 */
  GetApp = '/api/mobile/getApp',
  /** 获取所有移动端应用 */
  GetApps = '/api/mobile/getApps',

  /** 获取应用程序 */
  GetPackages = '/api/mobile/getPackages',

  /** 升级检测 */
  CheckUpgrade = '/api/mobile/checkUpgrade',

  /** 应用程序下载 */
  Download = '/api/mobile/download',
}

//#region 读取移动端应用
/**
 * 读取移动端应用
 * @param params 分页请求
 * @param mode 错误消息模型
 */
export function readApp(params: PageRequest, mode: ErrorMessageMode = 'message') {
  return request.post<PageData<AppOutputDtoSimple>>(
    {
      url: Api.ReadApp,
      params: params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
//#region 获取移动端应用
/**
 * 获取移动端应用
 * @param id 移动端应用编号
 * @param mode 错误消息模型
 */
export function getApp(id: string | bigint, mode: ErrorMessageMode = 'message') {
  return request.get<AppOutputDtoSimple>(
    {
      url: Api.GetApp,
      params: { id: id },
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
//#region 获取所有移动端应用
/**
 * 获取所有移动端应用
 * @param mode 错误消息模型
 */
export function getApps(mode: ErrorMessageMode = 'message') {
  return request.get<AppOutputDtoSimple[]>(
    {
      url: Api.GetApps,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 获取应用程序
/**
 * 获取应用程序
 * @param mode 错误消息模型
 */
export async function getPackages(mode: ErrorMessageMode = 'message') {
  return request.get<AjaxResult<AppPackageResultDto[]>>(
    {
      url: Api.GetPackages,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 升级检测
/**
 * 升级检测
 * @param dto DTO
 * @param mode 错误消息模型
 */
export async function checkUpgrade(dto: UpgradeCheckDto, mode: ErrorMessageMode = 'message') {
  return request.post<AjaxResult<AppPackageResultDto>>(
    {
      url: Api.CheckUpgrade,
      params: dto,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 应用程序下载
/**
 * 应用程序下载
 * @param packageId 应用程序包编号
 * @param mode 错误消息模型
 */
export async function download(packageId: string | bigint, mode: ErrorMessageMode = 'message') {
  return request.get(
    {
      url: Api.Download,
      params: { packageId: packageId },
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

export const appColumns: AdColumn[] = [
  {
    title: t('entity.idTitle'),
    field: 'Id',
    width: 180,
    formVisible: true,
    required: true,
    filterable: true,
    dataType: 'integer',
  },
  {
    title: t('mobile.route.app.codeText'),
    field: 'Code',
    width: 200,
    formVisible: true,
    required: true,
    filterable: true,
    dataType: 'string',
  },
  {
    title: t('mobile.route.app.platformText'),
    field: 'Platform',
    width: 150,
    formVisible: true,
    required: true,
    filterable: true,
    dataType: 'string',
  },
  {
    title: t('mobile.route.app.nameText'),
    field: 'Name',
    width: 250,
    formVisible: true,
    filterable: true,
    dataType: 'string',
  },
  {
    title: t('mobile.route.app.descriptionText'),
    field: 'Description',
    width: 300,
    formVisible: true,
    filterable: true,
    dataType: 'string',
  },
  // {
  //   title: t('mobile.route.app.iconText'),
  //   field: 'Icon',
  //   width: 70,
  //   visible: false,
  //   filterable: false,
  //   dataType: 'string',
  //   slots: { default: 'iconCol' },
  // },
  {
    title: t('mobile.route.authorText'),
    field: 'Author',
    width: 120,
    formVisible: true,
    filterable: true,
    dataType: 'string',
  },
  {
    title: t('mobile.route.emailText'),
    field: 'Email',
    width: 180,
    formVisible: true,
    filterable: true,
    dataType: 'string',
  },
  {
    title: t('mobile.route.phoneNumberText'),
    field: 'PhoneNumber',
    width: 160,
    formVisible: true,
    filterable: true,
    dataType: 'string',
  },
  {
    title: t('mobile.route.app.appStoreLinkText'),
    field: 'AppStoreLink',
    width: 120,
    formVisible: true,
    formEditable: true,
    filterable: true,
    dataType: 'boolean',
  },
  {
    title: t('mobile.route.app.urlText'),
    field: 'Url',
    width: 120,
    formVisible: true,
    filterable: true,
    dataType: 'string',
  },
  {
    title: t('entity.lockable.isLockedTitle'),
    field: 'IsLocked',
    width: 70,
    formVisible: true,
    formEditable: true,
    filterable: true,
    dataType: 'boolean',
  },
  {
    title: t('entity.creationAudited.creatorIdTitle'),
    field: 'CreatorId',
    width: 0,
    visible: false,
    formVisible: true,
    formEditable: false,
    filterable: true,
    dataType: 'integer',
  },
  {
    title: t('entity.creationAudited.creatorNameTitle'),
    field: 'CreatorName',
    width: 100,
    formVisible: true,
    formEditable: false,
    filterable: true,
    filterIndex: 'CreatorId',
    filterJoin: {
      entityType: 'EaCloud.Pack.Identity.Entities.User,EaCloud.Pack.Identity',
      filterField: 'UserName',
      selectField: 'Id',
    },
    dataType: 'string',
  },
  {
    title: t('entity.creationAudited.creatorNicknameTitle'),
    field: 'CreatorNickname',
    width: 100,
    formVisible: true,
    formEditable: false,
    filterable: true,
    filterIndex: 'CreatorId',
    filterJoin: {
      entityType: 'EaCloud.Pack.Identity.Entities.User,EaCloud.Pack.Identity',
      filterField: 'NickName',
      selectField: 'Id',
    },
    dataType: 'string',
  },
  {
    title: t('entity.creationAudited.createdTimeTitle'),
    field: 'CreatedTime',
    width: 180,
    formVisible: true,
    formEditable: false,
    filterable: true,
    dataType: 'datetime',
  },
  {
    title: t('entity.updateAudited.updaterIdTitle'),
    field: 'UpdaterId',
    width: 0,
    visible: false,
    formVisible: true,
    formEditable: false,
    filterable: true,
    dataType: 'integer',
  },
  {
    title: t('entity.updateAudited.updaterNameTitle'),
    field: 'UpdaterName',
    width: 100,
    formVisible: true,
    formEditable: false,
    filterable: true,
    filterIndex: 'UpdaterId',
    filterJoin: {
      entityType: 'EaCloud.Pack.Identity.Entities.User,EaCloud.Pack.Identity',
      filterField: 'UserName',
      selectField: 'Id',
    },
    dataType: 'string',
  },
  {
    title: t('entity.updateAudited.updaterNicknameTitle'),
    field: 'UpdaterNickname',
    width: 100,
    formVisible: true,
    formEditable: false,
    filterable: true,
    filterIndex: 'UpdaterId',
    filterJoin: {
      entityType: 'EaCloud.Pack.Identity.Entities.User,EaCloud.Pack.Identity',
      filterField: 'NickName',
      selectField: 'Id',
    },
    dataType: 'string',
  },
  {
    title: t('entity.updateAudited.updatedTimeTitle'),
    field: 'UpdatedTime',
    width: 180,
    formVisible: true,
    formEditable: false,
    filterable: true,
    dataType: 'datetime',
  },
];

/**
 * 高级选择列表
 * @param multiple 列表多选
 */
export function useAppChooseList(multiple: boolean = false) {
  const { dialogRegister, getDialogProps, getProps } = useAdChooseList({
    api: readApp,
    columns: appColumns,
    dialogId: 'ea_mobile_app_choose_list',
    dialogTitle: t('mobile.route.app.listText'),
    dialogMultiple: multiple,
  });

  return { AdChooseList, AdChooseListDialog, dialogRegister, getDialogProps, getProps };
}
